---
title: "HTML Report Demo"
output:
flexdashboard::flex_dashboard:
source_code: embed
social: menu
theme: flatly
---
```{r setup, include=FALSE}
library(flexdashboard)
library(knitr)
library(kableExtra)
knitr::opts_chunk$set(cache = TRUE, echo=F, eval=T, warning = FALSE, message = FALSE)
```
Workflow {.storyboard}
=========================================
Inputs {.sidebar}
-------------------------------------
Demo of an HTML report I generate and tailor to clients. They do not require any hosting or server to run. All the client needs to do is execute the HTML file and it will open in their local browser.
### **Interactive PCA Plot** {data-commentary-width=500}
```{r, fig.width=7}
pcaCars <- princomp(mtcars,cor = TRUE)
carsHC <- hclust(dist(pcaCars$scores),method = "ward.D2")
carsClusters <- cutree(carsHC,k=4)
carsDf <- data.frame(pcaCars$scores,"cluster"=factor(carsClusters))
carsDf <- transform(carsDf,cluster_name = paste("Cluster",carsClusters))
library(plotly)
p <- plot_ly(x=carsDf$Comp.1,y=carsDf$Comp.2,text=rownames(carsDf),
mode="markers",color = carsDf$cluster_name,marker=list(size=11))
p <- layout(p,title="PCA Clusters from Hierachical Clustering of Cars Data",
xaxis=list(title="PC1"),
yaxis=list(title="PC2"))
p
```
***
***Description***
Determine car model similarities by running data through a `Principle Component Analysis`.
An alternative solution is to use interactive plots that are usable from the `R` console, in the `RStudio` viewer pane, in R Markdown documents, and in `Shiny` apps. Annotations can be viewed by hovering the mouse pointer over a point or dragging a rectangle around the relevant area to zoom in. Interactive plots using `plotly` allow you to de-clutter the plotting area, include extra annotation information and create interactive web-based visualizations directly from R . Once uploaded to a `plotly` account, `plotly` graphs (and the data behind them) can be viewed and modified in a web browser.
### **Styled DataTable** {data-commentary-width=500}
```{r, fig.width=7}
mtcars %>%
kbl() %>%
kable_paper(full_width = F) %>%
column_spec(2, color = spec_color(mtcars$mpg[1:8]),
link = "https://haozhu233.github.io/kableExtra/") %>%
column_spec(6, color = "white",
background = spec_color(mtcars$drat[1:8], end = 0.7),
popover = paste("am:", mtcars$am[1:8]))
```
***
***Description***
Stylized table using `KableExtra`.
### **Animation Figures**{data-commentary-width=500}
```{r, fig.width=7}
library(plotly)
library(gapminder)
df <- gapminder
fig <- df %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
)
fig <- fig %>% layout(
xaxis = list(
type = "log"
)
)
fig <- fig %>% animation_opts(
1000, easing = "elastic", redraw = FALSE
)
fig <- fig %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
fig <- fig %>% animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="red"))
)
fig
```
***
***Description***
Animation of interactive figures.
### **Interactive Maps**{data-commentary-width=500}
```{r}
library(leaflet)
leaflet() %>%
addTiles() %>%
addMarkers(lng=-84.3880, lat=33.7537, popup="Natural Science Center")
```
***
***Description***
Leaflet is a JavaScript library for creating dynamic maps that support panning and zooming along with various annotations.
https://rstudio.github.io/leaflet/
- Interactive panning/zooming
- Compose maps using arbitrary combinations of map tiles, markers, polygons, lines, popups, and GeoJSON.
- Create maps right from the R console or RStudio
- Embed maps in knitr/R Markdown documents and Shiny apps
- Easily render Spatial objects from the sp package, or data frames with latitude/longitude columns
### **Duo Interactive Time Series**{data-commentary-width=500}
```{r, fig.width=7}
## Import libraries
library(plotly)
library(quantmod)
# Download some data
getSymbols(Symbols = c("AAPL", "MSFT"), from = '2018-01-01', to = '2019-01-01')
ds <- data.frame(Date = index(AAPL), AAPL[,6], MSFT[,6])
fig <- plot_ly(ds, x = ~Date)
fig <- fig %>% add_lines(y = ~AAPL.Adjusted, name = "Apple")
fig <- fig %>% add_lines(y = ~MSFT.Adjusted, name = "Microsoft")
fig <- fig %>% layout(
title = "Stock Prices",
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date")),
yaxis = list(title = "Price"))
fig
```
***
***Description***
Interactive time series tracking stock prices. Includes basic slider at the bottom of the figure. Time frame selectors buttons provided at the top of the figure.
Session info
=========================================
Column
---------------------------------------------
```{r}
sessionInfo()
```